using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace HandyIRCChat
{
    public class OptionsPropertyDescriptorWrapper : PropertyDescriptor
    {
        private PropertyDescriptor propertyDescriptor;

        private Options optionsInstance;

        private OptionsInfoAttribute optionsInfoAttribute;

        private bool hidden = false;

        public OptionsPropertyDescriptorWrapper(Options opt, 
            PropertyDescriptor propDescriptor) : base(propDescriptor)
        {
            optionsInstance = opt;
            propertyDescriptor = propDescriptor;

            Type type = typeof(Options);
            foreach (PropertyInfo pInfo in type.GetProperties())
            {
                OptionsInfoAttribute attr =
                    (OptionsInfoAttribute)Attribute.GetCustomAttribute(pInfo, typeof(OptionsInfoAttribute));

                if (attr != null && attr.Group == OptionsGroup.None)
                {
                    hidden = true;
                    break;
                }

                if (attr != null  && propertyDescriptor.Name == pInfo.Name)
                {
                    optionsInfoAttribute = attr;
                    break;
                }
            }
        }

        public bool IsHidden
        {
            get
            {
                return hidden;
            }
        }

        public override string DisplayName
        {
            get
            {
                return optionsInfoAttribute.Name;
            }
        }

        public override string Description
        {
            get
            {
                return optionsInfoAttribute.Description;
            }
        }

        public override string Category
        {
            get
            {
                return propertyDescriptor.Category;
            }
        }

        public override Type ComponentType
        {
            get
            {
                return propertyDescriptor.ComponentType;
            }
        }

        public override bool IsReadOnly
        {
            get
            {
                return propertyDescriptor.IsReadOnly;
            }
        }

        public override Type PropertyType
        {
            get
            {
                return propertyDescriptor.PropertyType;
            }
        }

        public override bool CanResetValue(object component)
        {
            return propertyDescriptor.CanResetValue(optionsInstance);
        }

        public override object GetValue(object component)
        {
            return propertyDescriptor.GetValue(optionsInstance);
        }

        public override void ResetValue(object component)
        {
            propertyDescriptor.ResetValue(optionsInstance);
        }

        public override void SetValue(object component, object value)
        {
            propertyDescriptor.SetValue(optionsInstance, value);
        }

        public override bool ShouldSerializeValue(object component)
        {
            return propertyDescriptor.ShouldSerializeValue(optionsInstance);
        }

        public override object GetEditor(Type editorBaseType)
        {
            return propertyDescriptor.GetEditor(editorBaseType);
        }

        public override TypeConverter Converter
        {
            get
            {
                return propertyDescriptor.Converter;
            }
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return propertyDescriptor.Attributes;
            }
        }
    }
}
